package com.ihrm.salarys.controller;


import com.ihrm.common.controller.BaseController;
import com.ihrm.common.entity.Result;
import com.ihrm.common.entity.ResultCode;
import com.ihrm.domain.salarys.SalaryArchive;
import com.ihrm.domain.salarys.SalaryArchiveDetail;
import com.ihrm.salarys.service.ArchiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 归档Controller
 */
@CrossOrigin
@RestController
@RequestMapping(value = "/salarys")
public class ArchiveController extends BaseController {
    
    @Autowired
    private ArchiveService archiveService;
    
    /**
     * 制作薪资报表
     */
    @RequestMapping(value = "/reports/{yearMonth}",method = RequestMethod.GET)
    public Result historyDetail(@PathVariable String yearMonth,int onType){
    
        List<SalaryArchiveDetail> list = new ArrayList<>();
        /**
         * 判断onType参数:
         *      如果==1，自己构造数据表
         *      如果!=1,查询主表数据
         */
        if(onType==1){
           list = archiveService.getReports(yearMonth,companyId);
        }else{
            /**
             * 查询主表数据
             */
            SalaryArchive sa = archiveService.findSalaryArchive(yearMonth, companyId);
            /**
             * 根据主表的id,查询明细表的所有数据
             */
            if(sa!=null){
                list = archiveService.findSalaryArchiveDetail(sa.getId());
            }
        }
        return  new Result(ResultCode.SUCCESS,list);
    
    }
    
}
